<?php
namespace app\common\taglib;

use think\template\TagLib;

class Html extends TagLib
{
    /**
     * 定义标签列表
     */
    protected $tags   =  [
        'config'  => array(
            'attr' => 'name',
            'close' => 0
        ),
        'list'	=> array(
            'attr'	=> 'id,cid,order,limit',
            'close'	=> 1,
        ),
		
		'nav'	=> array(
		    'attr'	=> 'id',
		    'close'	=> 1,
		),
		'index'	=> array(
		    'attr'	=> 'id',
		    'close'	=> 1,
		),
		'cate'	=> array(
		    'attr'	=> 'id,cid,type',
		    'close'	=> 1,
		),
		'catelist'	=> array(
		    'attr'	=> 'id,cid,type',
		    'close'	=> 1,
		),
		'crumb'	=> array(
		    'attr'	=> 'cid',
		    'close'	=> 0,
		),
		'banner'	=> array(
		    'attr'	=> 'id,order,limit',
		    'close'	=> 1,
		),
		'link'	=> array(
		    'attr'	=> 'id,order,limit',
		    'close'	=> 1,
		),
		'tag'	=> array(
		    'attr'	=> 'id,order,limit',
		    'close'	=> 1,
		)
    ];

    public function tagConfig($attr)
    {
        return site_config($attr['name']);
    }
    public function tagList($attr, $content)
    {
        $cid   = !isset($attr['cid']) || $attr['cid'] == '' ? 0 : $attr['cid'];
        $order = empty($attr['order']) ? "id DESC" : $attr['order'];
        $limit = empty($attr['limit']) ? 10 : $attr['limit'];
		$size = empty($attr['size']) ? 0 : $attr['size'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
		if($cid){
			$cate=model("cate")->where('pid',$cid)->find();
			if(empty($cate)){
				$str.="\$map['cid']=$cid ;";
			}else{
				$str.="\$map['cid']=get_cate_ids($cid) ;";
			}
			
		}
		if($size){
			$str .= "\$__LIST__ = model('post')->where(\$map)->order('$order')->paginate({$size},false,['path'=>'/'.input('name')]) ;";
		}else{
			$str .= "\$__LIST__ = model('post')->where(\$map)->order('$order')->limit('$limit')->select() ;";
		}
		$str .= "?>";
		$str .= "<ul class='clear'><volist name='__LIST__'  id='$id'>";
        $str .= $content;
        $str .= '</volist></ul>';
		if($size){
			$str .= "<div class='page'>{:page_url(\$__LIST__->render())}</div>";
		}
        return $str;
    }
	public function tagNav($attr, $content)
	{
	    
	    $order = "sort=0,sort,id";
		$id    = empty($attr['id']) ? 'nav' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
		$str.="\$map['nav_on']=1 ;";
	    $str .= "\$__LIST__ =list_to_tree(model('cate')->where(\$map)->orderRaw('$order')->select()->toArray());";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
	    return $str;
	}
	public function tagIndex($attr, $content)
	{
	    
	    $order = "sort=0,sort,id";
		$id    = empty($attr['id']) ? 'nav' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
		$str.="\$map['index_on']=1 ;";
	    $str .= "\$__LIST__ =list_to_tree(model('cate')->where(\$map)->orderRaw('$order')->select()->toArray());";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
	    return $str;
	}
	public function tagCate($attr, $content)
	{
		
	    $cid   = empty($attr['cid']) ? 0 : $attr['cid'];
		$type   = empty($attr['type']) ? '' : $attr['type'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
		if($type == 'parent'){
			$str.="\$cate=db_find('cate',['id'=>$cid]);";
			$str.="empty(\$cate['pid'])? \$map['id']=$cid: \$map['id'] = \$cate['pid'] ;";
		}else{
			$str.="\$map['id']=$cid ;";
		}
	 
	    $str .= "\$__LIST__ = model('cate')->where(\$map)->limit(1)->select() ;";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
		
	    return $str;
	}
	
	public function tagCatelist($attr, $content)
	{
		
	    $cid   = empty($attr['cid']) ? 0 : $attr['cid'];
		$type   = empty($attr['type']) ? '' : $attr['type'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
		if($type == 'parent'){
			$str.="\$cate=db_find('cate',['id'=>$cid]);";
			$str.="empty(\$cate['pid'])? \$map['pid']=$cid: \$map['pid'] = \$cate['pid'] ;";
		}else{
			$str.="\$map['pid']=$cid ;";
		}
		
	    $str .= "\$__LIST__ = db('cate')->where(\$map)->orderRaw('sort=0,sort,id')->select() ;";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
		
	    return $str;
	}
	
	public function tagCrumb($attr)
	{
	    $cid   = !isset($attr['cid']) || $attr['cid'] == '' ? 0 : $attr['cid'];
		$str="<?php ";
		$str.= "echo '<a href=\"/\">首页</a>';";
	    $str.="\$list=get_cate_pid($cid) ;";
		$str .= "?>";
		$str.="<volist name='list'  id='cate_id'>";
		$str.="<php>\$cate_vo=db_find('cate','id='.\$cate_id);</php>";
		$str.="<span>&gt;</span><a href=\"{\$cate_vo['url']}\">{\$cate_vo['name']}</a>";
		$str .= "</volist>";
	    return $str;
	}
	public function tagBanner($attr, $content)
	{
	    $order = empty($attr['order']) ? "sort=0,sort,id " : $attr['order'];
		$limit = empty($attr['limit']) ? 5 : $attr['limit'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
	    $str .= "\$__LIST__ = db('banner')->where(\$map)->orderRaw('$order')->limit('$limit')->select() ;";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
	    return $str;
	}
	public function tagLink($attr, $content)
	{
	    $order = empty($attr['order']) ? "id DESC" : $attr['order'];
		$limit = empty($attr['limit']) ? 10 : $attr['limit'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
	    $str .= "\$__LIST__ = db('link')->where(\$map)->orderRaw('$order')->limit('$limit')->select() ;";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
	    return $str;
	}
	public function tagTag($attr, $content)
	{
	    $order = empty($attr['order']) ? "id DESC" : $attr['order'];
		$limit = empty($attr['limit']) ? 10 : $attr['limit'];
		$id    = empty($attr['id']) ? 'vo' : $attr['id'];
		$str="<?php ";
		$str.="\$map=[] ;";
	    $str .= "\$__LIST__ = db('tag')->where(\$map)->orderRaw('$order')->limit('$limit')->select() ;";
		$str .= "?>";
		$str .= "<volist name='__LIST__'  id='$id'>";
	    $str .= $content;
	    $str .= '</volist>';
	    return $str;
	}
}
